<?php
/**
 * @var $this ImageWidget
 */
if (!empty($this->path))
    $connectorPath = 'index.php?r=Core/service/command&SID=Core.FileManager.cmdConnect&root=' . $this->path;
else $connectorPath = 'index.php?r=Core/service/command&SID=Core.FileManager.cmdConnect&root=uploads/';
//endmodified
$script = "
\$('.form').delegate('.remove-image', 'click', function(){
    \$(this).closest('.images').next().html('Select Image');            
    \$(this).closest('.image').remove();    
    return false;
});
var trigger = null;
\$('.form').delegate('.add-image, .attach-image', 'click', function(){
    trigger = \$(this);
    if(\$('#finder2').length == 0) {
        \$('body').append(\$('<div/>').attr('id', 'finder2'));
        \$('#finder2').elfinder({
            zIndex: 9999999, 
            places: 'Favorites', 
            url : '" . $connectorPath . "',
            lang : 'en',
            dialog : { width : 900, modal : true, title : 'File Manager', zIndex: 7777777 },
            editorCallback : function(url) {
                //check duplicate
                var container = \$('div.images');
                if (container.length)
                {
                    var items = \$('.image', container);
                    if (items.length)
                    {
                        var fileExist = false;
                        \$.each(items, function(index, item){
                            if (url == \$('input', item).val())
                            {
                                fileExist = true;
                                return false;
                            }
                        });
                        if (fileExist)
                        {
                            alert('File is already selected.');
                            return false;
                        }
                    }
                }
                
                var baseUrl = '" . baseUrl() . "';
                if (trigger.hasClass('attach-image'))
                {
                    var item = trigger.closest('.image');
                    \$('img', item).attr('src', url);
                    \$('input', item).val(url.replace(baseUrl,''));
                }
                else if (trigger.hasClass('add-image'))
                {     
                    if (container.length <= 0)
                    {
                        container = \$('<div class=\"images\"></div>');
                        trigger.before(container);     
                    }
                    else{
                        \$('a.add-image').html('Change Image');
                    }
                    var item = \$('<div class=\"image\"></div>');
                    container.html(item);
                    
                    item
                        .append('<input type=\"hidden\" name=\"" . get_class($model) . "[{$this->imageAttribute}]\" value=\"'+url.replace(baseUrl,'')+'\" />')
                        .append('<a href=\"#\" title=\"Change image\" class=\"attach-image\"><img src=\"'+url+'\" width=\"100\" height=\"100\" /></a>')
                        .append('<a href=\"#\" title=\"Remove\" class=\"remove-image\">Remove</a>');
                                            
                }
            },
            closeOnEditorCallback: true
        });
    }
    else {
        \$('#finder2').elfinder('open');
    };
    return false;
});
";

$cs = Yii::app()->clientScript;
$cs->registerScript(__CLASS__ . '#AttachImage', $script, CClientScript::POS_READY);
$cs->registerCoreScript('jquery.ui');
$asset = Yii::app()->core->AssetUrl . '/elfinder-1.1';
$cs->registerScriptFile($asset . '/js/elfinder.min.js', CClientScript::POS_BEGIN);
$cs->registerCssFile($asset . '/css/elfinder.css');
$cs->registerCssFile(Yii::app()->theme->baseUrl . '/styles/widgets/jquery.ui.dialog.css');
?>

<?php //if (count($model->images)):?>
<div class="images">
    <?php //foreach ($model->images as $src):?>
    <?php
    $src = $model->{$this->imageAttribute};
    if (empty($src) === false):?>
        <div class="image">
            <?php echo $form->hiddenField($model, $this->imageAttribute, array('value' => $src)); ?>
            <?php echo CHtml::link(CHtml::image(baseUrl() . '/' . $src, 'Change Image', array('width' => 100, 'height' => 100)), '#', array('class' => 'attach-image')); ?>
            <?php echo CHtml::link('Remove', '#', array('class' => 'remove-image')); ?>
        </div>
        <?php endif;?>
    <?php //endforeach;?>
</div>
<?php //endif;?>
<?php echo CHtml::link(empty($src) ? 'Select Image' : 'Change Image', '#', array('class' => 'add-image')); ?>
